<?php
class PriviledgeController extends Zend_Controller_Action
{
	  public function init()
	  {
		  IndexController::initAdmin($this);
	  }
		
	  public function indexAction()
	  {	      
      $arrResult	= QPriviledge::search($this->AppUI->country);
		  //post METHOD	
		  if($this->_request->isPost())
		  {
         foreach ($arrResult as $value)
         {
            $admin = $this->_request->getParam('admin_'.$value["id"], null);
            $inventory = $this->_request->getParam('inventory_'.$value["id"], null);
            $marketing = $this->_request->getParam('marketing_'.$value["id"], null);
            $buying = $this->_request->getParam('buying_'.$value["id"], null);
            
            $arrParams	= array
					  (
						  'id'              =>  $value["id"],
						  'group'           =>  $value["group"],
						  'function'        =>  $value["function"],
						  'admin'			=>  $admin == "on"  ? 1 : 0,
						  'inventory'		=>  $inventory == "on" ? 1 : 0,
						  'marketing'		=>  $marketing == "on" ? 1 : 0,
						  'buying'			=>  $buying == "on" ? 1 : 0,
					  );
          
            QPriviledge::update($arrParams , $this->AppUI->country);
         }
      }

      $arrResult	= QPriviledge::search($this->AppUI->country);
 	   
      $this->view->arrayGroup = CConfig::$priviledgeGroup;
      $this->view->arrayFunc = CConfig::$priviledgeFunction;
		  $this->view->arrResult	= $arrResult;
	  }
    public static function checkPriviledge($userinGroup, $group, $function, $country)
	  {  
          $value	= QPriviledge::checkPriviledge($userinGroup, $group, $function, $country);
          
          if($value==NULL)
          {
            return false;
          }
          if($userinGroup == (string)GROUP_Administrator)  //GROUP_Administrator
          {
             if($value[0]["admin"]== "1")
              {
                return true;
              }
          }
          else if($userinGroup == (string)GROUP_Buying)  //GROUP_Buying
          {
              if($value[0]["buying"]=="1")
              {
                return true;
              }
          }
          else if($userinGroup == (string)GROUP_Inventory)  //GROUP_Inventory
          {
              if($value[0]["inventory"]=="1")
              {
                return true;
              }
          }
          else if($userinGroup == (string)GROUP_Marketing)  //GROUP_Marketing
          {
              if($value[0]["marketing"]=="1")
              {
                return true;
              }
          }
	  		  return false;
	  }
}
?>
